//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jeecg.modules.online.cgreport.a;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.DynamicDataSourceModel;
import org.jeecg.common.util.BrowserUtils;
import org.jeecg.common.util.SqlInjectionUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.util.security.JdbcSecurityUtil;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportHead;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportItem;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportParam;
import org.jeecg.modules.online.cgreport.mapper.OnlCgreportHeadMapper;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportItemService;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportParamService;
import org.jeecg.modules.online.cgreport.service.a.b;
import org.jeecg.modules.online.cgreport.service.a.c;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("onlCgreportAPI")
@RequestMapping({"/online/cgreport/api"})
public class a {
    private static final Logger a = LoggerFactory.getLogger(a.class);
    @Autowired
    private b cgreportAPIService;
    @Autowired
    private c onlCgreportHeadService;
    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;
    @Lazy
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;
    @Autowired
    private org.jeecg.modules.online.config.c.a onlReportQueryBlackListHandler;

    public a() {
    }

    @GetMapping({"/getColumnsAndData/{code}"})
    @PermissionData
    public Result<?> a(@PathVariable("code") String var1, HttpServletRequest var2) {
        OnlCgreportHead var3 = (OnlCgreportHead)this.onlCgreportHeadService.getById(var1);
        if (var3 == null) {
            return Result.error("实体不存在");
        } else {
            Result var4 = this.b(var1, var2);
            if (!var4.getCode().equals(200)) {
                return var4;
            } else {
                Map var5 = (Map)var4.getResult();
                List var6 = (List)var5.get("records");
                Map var7 = this.onlCgreportHeadService.queryColumnInfo(var1, false);
                JSONArray var8 = (JSONArray)var7.get("columns");
                HashMap var9 = new HashMap(5);
                if (var8 != null) {
                    for(int var10 = 0; var10 < var8.size(); ++var10) {
                        JSONObject var11 = var8.getJSONObject(var10);
                        Object var12 = var11.get("dictCode");
                        if (var12 != null) {
                            String var13 = var12.toString();
                            String var14 = var8.getJSONObject(var10).getString("dataIndex");
                            List var15 = this.onlCgreportHeadService.queryColumnDictList(var13, var6, var14);
                            if (var15 != null) {
                                var9.put(var14, var15);
                                var11.put("customRender", var14);
                            }
                        }
                    }
                }

                var7.put("cgreportHeadName", var3.getName());
                var7.put("data", var4.getResult());
                var7.put("dictOptions", var9);
                return Result.ok(var7);
            }
        }
    }
    @GetMapping({"/getColumnsAndData/withoutDict/{code}"})
    @PermissionData
    public Result<?> a2(@PathVariable("code") String var1, HttpServletRequest var2) {
        OnlCgreportHead var3 = (OnlCgreportHead)this.onlCgreportHeadService.getById(var1);
        if (var3 == null) {
            return Result.error("实体不存在");
        } else {
            Map var7 = this.onlCgreportHeadService.queryColumnInfo(var1, false);
            HashMap var9 = new HashMap(5);
            var7.put("cgreportHeadName", var3.getName());
            var7.put("dictOptions", var9);
            return Result.ok(var7);
        }
    }
    /** @deprecated */
    @Deprecated
    @GetMapping({"/getColumns/{code}"})
    public Result<?> a(@PathVariable("code") String var1) {
        OnlCgreportHead var2 = (OnlCgreportHead)this.onlCgreportHeadService.getById(var1);
        if (var2 == null) {
            return Result.error("实体不存在");
        } else {
            QueryWrapper var3 = new QueryWrapper();
            var3.eq("cgrhead_id", var1);
            var3.eq("is_show", 1);
            var3.orderByAsc("order_num");
            List var4 = this.onlCgreportItemService.list(var3);
            ArrayList var5 = new ArrayList();
            HashMap var6 = new HashMap(5);
            Iterator var7 = var4.iterator();

            while(var7.hasNext()) {
                OnlCgreportItem var8 = (OnlCgreportItem)var7.next();
                HashMap var9 = new HashMap(5);
                var9.put("title", var8.getFieldTxt());
                var9.put("dataIndex", var8.getFieldName());
                var9.put("align", "center");
                var9.put("sorter", "true");
                var5.add(var9);
                String var10 = var8.getDictCode();
                if (oConvertUtils.isNotEmpty(var10)) {
                    List var11 = null;
                    if (var10.toLowerCase().indexOf("select ") == 0) {
                        this.sysBaseAPI.dictTableWhiteListCheckByDict(var10, new String[0]);
                        SqlInjectionUtil.specialFilterContentForOnlineReport(var10);
                        List var12 = ((OnlCgreportHeadMapper)this.onlCgreportHeadService.getBaseMapper()).executeSqlDict(var10);
                        if (var12 != null && var12.size() != 0) {
                            String var13 = JSON.toJSONString(var12);
                            var11 = JSON.parseArray(var13, DictModel.class);
                        }
                    } else {
                        var11 = this.sysBaseAPI.queryDictItemsByCode(var10);
                    }

                    if (var11 != null) {
                        var6.put(var8.getFieldName(), var11);
                        var9.put("customRender", var8.getFieldName());
                    }
                }
            }

            HashMap var14 = new HashMap(1);
            var14.put("columns", var5);
            var14.put("dictOptions", var6);
            var14.put("cgreportHeadName", var2.getName());
            return Result.ok(var14);
        }
    }

    @GetMapping({"/getData/{code}"})
    @PermissionData
    public Result<?> b(@PathVariable("code") String var1, HttpServletRequest var2) {
        Map var3 = org.jeecg.modules.online.cgreport.c.b.a(var2);
        var3.put("getAll", var2.getAttribute("getAll"));

        try {
            return Result.OK(this.cgreportAPIService.getDataById(var1, var3));
        } catch (JeecgBootException var5) {
            return Result.error(var5.getMessage());
        }
    }

    @GetMapping({"/getDataOrderByValue/{code}"})
    @PermissionData
    public Result<?> c(@PathVariable("code") String var1, HttpServletRequest var2) {
        OnlCgreportHead var3 = (OnlCgreportHead)this.onlCgreportHeadService.getById(var1);
        if (var3 == null) {
            return Result.error("实体不存在");
        } else {
            String var4 = var3.getCgrSql().trim();
            String var5 = var3.getDbSource();

            try {
                Map var6 = org.jeecg.modules.online.cgreport.c.b.a(var2);
                Object var7 = var6.get("order_field");
                Object var8 = var6.get("order_value");
                if (!oConvertUtils.isEmpty(var7) && !oConvertUtils.isEmpty(var8)) {
                    String var9 = "force_" + var7;
                    var6.put(var9, var8);
                    var6.put("getAll", true);
                    Map var10 = this.cgreportAPIService.executeSelectSqlRoute(var5, var4, var6, var3.getId());
                    JSONArray var11 = JSON.parseArray(JSON.toJSONString(var10.get("records")));
                    var6.remove(var7.toString());
                    var6.remove(var9);
                    var6.remove("order_field");
                    var6.remove("order_value");
                    var6.put("getAll", var2.getAttribute("getAll"));
                    Map var12 = this.cgreportAPIService.executeSelectSqlRoute(var5, var4, var6, var3.getId());
                    JSONArray var13 = JSON.parseArray(JSON.toJSONString(var12.get("records")));
                    this.a(var11, var13);
                    var12.put("records", var13);
                    return Result.ok(var12);
                } else {
                    return Result.error("order_field 和 order_value 参数不能为空！");
                }
            } catch (Exception var14) {
                a.error(var14.getMessage(), var14);
                return Result.error("SQL执行失败：" + var14.getMessage());
            }
        }
    }

    private void a(JSONArray var1, JSONArray var2) {
        for(int var3 = 0; var3 < var1.size(); ++var3) {
            JSONObject var4 = var1.getJSONObject(var3);
            String var5 = var4.getString("id");
            int var6 = (int)var2.stream().filter((var1x) -> {
                return var5.equals(((JSONObject)var1x).getString("id"));
            }).count();
            if (var6 == 0) {
                var2.add(0, var4);
            }
        }

    }

    @GetMapping({"/getQueryInfo/{code}"})
    public Result<?> b(@PathVariable("code") String var1) {
        try {
            List var2 = this.onlCgreportItemService.getAutoListQueryInfo(var1);
            return Result.ok(var2);
        } catch (Exception var3) {
            return Result.error("查询失败");
        }
    }

    @GetMapping({"/getParamsInfo/{code}"})
    public Result<?> c(@PathVariable("code") String var1) {
        try {
            LambdaQueryWrapper<OnlCgreportParam> var2 = new LambdaQueryWrapper<OnlCgreportParam>();
            var2.eq(OnlCgreportParam::getCgrheadId, var1);
            var2.orderByAsc(OnlCgreportParam::getOrderNum);
            List var3 = this.onlCgreportParamService.list(var2);
            return Result.ok(var3);
        } catch (Exception var4) {
            return Result.error("查询失败");
        }
    }

    @PermissionData
    @RequestMapping({"/exportManySheetXls/{reportId}"})
    public void a(@PathVariable("reportId") String var1, HttpServletRequest var2, HttpServletResponse var3) {
        if (oConvertUtils.isEmpty(var1)) {
            throw new JeecgBootException("参数错误");
        } else {
            Map var4 = org.jeecg.modules.online.cgreport.c.b.a(var2);
            Workbook var5 = this.cgreportAPIService.getReportWorkbook(var1, var4);
            String var6 = "报表";
            var3.setContentType("application/vnd.ms-excel");
            ServletOutputStream var7 = null;

            try {
                String var8 = BrowserUtils.checkBrowse(var2);
                if ("MSIE".equalsIgnoreCase(var8.substring(0, 4))) {
                    var3.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(var6, "UTF-8") + ".xls");
                } else {
                    String var9 = new String(var6.getBytes("UTF-8"), "ISO8859-1");
                    var3.setHeader("content-disposition", "attachment;filename=" + var9 + ".xls");
                }

                var7 = var3.getOutputStream();
                var5.write(var7);
            } catch (Exception var18) {
                a.warn("导出失败", var18.getMessage());
            } finally {
                try {
                    var7.flush();
                    var7.close();
                } catch (Exception var17) {
                }

            }

        }
    }

    /** @deprecated */
    @Deprecated
    @PermissionData
    @RequestMapping({"/exportXls/{reportId}"})
    public void b(@PathVariable("reportId") String var1, HttpServletRequest var2, HttpServletResponse var3) {
        String var4 = "报表";
        String var5 = "导出信息";
        if (!oConvertUtils.isNotEmpty(var1)) {
            throw new JeecgBootException("参数错误");
        } else {
            Map var6 = null;

            try {
                var6 = this.onlCgreportHeadService.queryCgReportConfig(var1);
            } catch (Exception var38) {
                throw new JeecgBootException("动态报表配置不存在!");
            }

            List var7 = (List)var6.get("items");
            var2.setAttribute("getAll", true);
            Result var8 = this.b(var1, var2);
            List var9 = null;
            if (var8.getCode().equals(200)) {
                Map var10 = (Map)var8.getResult();
                var9 = (List)var10.get("records");
            }

            ArrayList var39 = new ArrayList();
            HashMap var11 = new HashMap(5);
            HashMap var12 = new HashMap(5);
            ArrayList var13 = new ArrayList();

            String var16;
            String var19;
            for(int var14 = 0; var14 < var7.size(); ++var14) {
                Map var15 = (Map)var7.get(var14);
                var16 = (String)var15.get("field_type");
                if ("1".equals(oConvertUtils.getString(((Map)var7.get(var14)).get("is_show")))) {
                    String var17 = ((Map)var7.get(var14)).get("field_name").toString();
                    ExcelExportEntity var18 = new ExcelExportEntity(((Map)var7.get(var14)).get("field_txt").toString(), var17, 15);
                    var19 = "_";
                    String var20 = "---";
                    Object var21 = ((Map)var7.get(var14)).get("dict_code");
                    List var22 = this.onlCgreportHeadService.queryColumnDictList(oConvertUtils.getString(var21), var9, var17);
                    if (var22 != null && var22.size() > 0) {
                        ArrayList var23 = new ArrayList();
                        Iterator var24 = var22.iterator();

                        while(var24.hasNext()) {
                            DictModel var25 = (DictModel)var24.next();
                            if (var25.getValue().contains(var19)) {
                                String var26 = var25.getValue().replace(var19, var20);
                                var23.add(var25.getText() + var19 + var26);
                            } else {
                                var23.add(var25.getText() + var19 + var25.getValue());
                            }
                        }

                        var18.setReplace((String[])var23.toArray(new String[var23.size()]));
                    }

                    Object var52 = ((Map)var7.get(var14)).get("replace_val");
                    if (oConvertUtils.isNotEmpty(var52)) {
                        var18.setReplace(var52.toString().split(","));
                    }

                    if (oConvertUtils.isNotEmpty(((Map)var7.get(var14)).get("group_title"))) {
                        String var53 = ((Map)var7.get(var14)).get("group_title").toString();
                        Object var54 = new ArrayList();
                        if (var12.containsKey(var53)) {
                            var54 = (List)var12.get(var53);
                            ((List)var54).add(var17);
                        } else {
                            ExcelExportEntity var55 = new ExcelExportEntity(var53, var53, true);
                            var13.add(var55);
                            ((List)var54).add(var17);
                        }

                        var12.put(var53, var54);
                        var18.setColspan(true);
                    }

                    if (oConvertUtils.isNotEmpty(var16) && oConvertUtils.isEmpty(var21) && ("Integer".equals(var16) || "Long".equals(var16))) {
                        var18.setType(4);
                    }

                    var13.add(var18);
                }

                if ("1".equals(oConvertUtils.getString(((Map)var7.get(var14)).get("is_total")))) {
                    var39.add(((Map)var7.get(var14)).get("field_name").toString());
                }
            }

            Iterator var40 = var12.entrySet().iterator();

            while(var40.hasNext()) {
                Entry var41 = (Entry)var40.next();
                var16 = (String)var41.getKey();
                List var45 = (List)var41.getValue();
                Iterator var48 = var13.iterator();

                while(var48.hasNext()) {
                    ExcelExportEntity var50 = (ExcelExportEntity)var48.next();
                    if (var16.equals(var50.getName()) && var50.isColspan()) {
                        var50.setSubColumnList(var45);
                    }
                }
            }

            String var43;
            if (var39 != null && var39.size() > 0) {
                var40 = var39.iterator();

                while(var40.hasNext()) {
                    var43 = (String)var40.next();
                    BigDecimal var44 = new BigDecimal(0.0D);
                    Iterator var46 = var9.iterator();

                    while(var46.hasNext()) {
                        Map var49 = (Map)var46.next();
                        var19 = var49.get(var43).toString();
                        if (var19.matches("\\d+(.\\d+)?")) {
                            BigDecimal var51 = new BigDecimal(var19);
                            var44 = var44.add(var51);
                        }
                    }

                    var11.put(var43, var44);
                }

                var9.add(var11);
            }

            var3.setContentType("application/vnd.ms-excel");
            ServletOutputStream var42 = null;

            try {
                var43 = BrowserUtils.checkBrowse(var2);
                if ("MSIE".equalsIgnoreCase(var43.substring(0, 4))) {
                    var3.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(var4, "UTF-8") + ".xls");
                } else {
                    var16 = new String(var4.getBytes("UTF-8"), "ISO8859-1");
                    var3.setHeader("content-disposition", "attachment;filename=" + var16 + ".xls");
                }

                Workbook var47 = ExcelExportUtil.exportExcel(new ExportParams((String)null, var5), var13, var9);
                var42 = var3.getOutputStream();
                var47.write(var42);
            } catch (Exception var36) {
            } finally {
                try {
                    var42.flush();
                    var42.close();
                } catch (Exception var35) {
                }

            }

        }
    }

    @GetMapping({"/getRpColumns/{code}"})
    public Result<?> d(@PathVariable("code") String var1) {
        LambdaQueryWrapper<OnlCgreportHead> var2 = new LambdaQueryWrapper<OnlCgreportHead>();
        var2.eq(OnlCgreportHead::getCode, var1);
        OnlCgreportHead var3 = (OnlCgreportHead)this.onlCgreportHeadService.getOne(var2);
        if (var3 == null) {
            return Result.error("实体不存在");
        } else {
            Map var4 = this.onlCgreportHeadService.queryColumnInfo(var3.getId(), true);
            var4.put("cgRpConfigId", var3.getId());
            var4.put("cgRpConfigName", var3.getName());
            return Result.ok(var4);
        }
    }

    @PostMapping({"/testConnection"})
    public Result a(@RequestBody DynamicDataSourceModel var1) {
        Connection var2 = null;

        Result var4;
        try {
            JdbcSecurityUtil.validate(var1.getDbUrl());
            Class.forName(var1.getDbDriver());
            var2 = DriverManager.getConnection(var1.getDbUrl(), var1.getDbUsername(), var1.getDbPassword());
            Result var3;
            if (var2 != null) {
                var3 = Result.ok("数据库连接成功");
                return var3;
            }

            var3 = Result.ok("数据库连接失败：错误未知");
            return var3;
        } catch (ClassNotFoundException var17) {
            a.error(var17.toString());
            var4 = Result.error("数据库连接失败：驱动类不存在");
            return var4;
        } catch (Exception var18) {
            a.error(var18.toString());
            var4 = Result.error("数据库连接失败：" + var18.getMessage());
        } finally {
            try {
                if (var2 != null && !var2.isClosed()) {
                    var2.close();
                }
            } catch (SQLException var16) {
                a.error(var16.toString());
            }

        }

        return var4;
    }

    @GetMapping({"/getReportDictList"})
    @RequiresPermissions({"online:report:getDictList"})
    public Result<?> a(@RequestParam("fieldId") String var1, @RequestParam(name = "keyword",required = false) String var2) {
        OnlCgreportItem var3 = (OnlCgreportItem)this.onlCgreportItemService.getById(var1);
        if (var3 == null) {
            throw new JeecgBootException("指定字段不存在");
        } else {
            String var4 = var3.getDictCode();
            List var5 = this.onlCgreportHeadService.queryDictSelectData(var4, var2);
            return Result.ok(var5);
        }
    }
}
